//
//  YeuThichViewController.m
//  NhomTuan
//
//  Created by Buffalo on 1/30/15.
//  Copyright (c) 2015 Buffalo. All rights reserved.
//

#import "YeuThichViewController.h"
#import "YeuThichTableViewCell.h"
@interface YeuThichViewController ()

@end
NSString *songTag;

@implementation YeuThichViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
    self.tableYeuThich.delegate = self;
    self.tableYeuThich.dataSource = self;

    [self.navigationController.navigationBar setBarTintColor:[AppUtils greenColor]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    UIImage *image = [UIImage imageNamed:@"img_logo.png"];
    UIImageView *titleImg = [[UIImageView alloc] initWithImage:image];
    [self.navigationController.navigationBar.topItem setTitleView:titleImg];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (IBAction)btnUnLike:(UIButton *)sender {
    songTag = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    Song *song = [[Song alloc] initSongWithCode:songTag];

    NSString *message = [NSString stringWithFormat:@"Bạn có đồng ý bỏ thích bài hát: \n'%@' ?", song.name];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bỏ thích bài hát?"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"Hủy"
                                          otherButtonTitles:@"Đồng ý", nil];
    alert.tag = 100;
    [alert show];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"yeu_thich_cell_id";
    YeuThichTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    NSString *songCode = [AppManager defaultmanager].favorSongList[indexPath.row];
    Song *favorSong = [[Song alloc] initSongWithCode:songCode];

    cell.lblSongCode.text = favorSong.code;
    cell.lblSongLyrics.text = favorSong.lyrics;
    cell.lblSongName.text = favorSong.name;
    cell.btnLike.tag = [favorSong.code integerValue];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [AppManager defaultmanager].favorSongList.count;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 100) {
        if (buttonIndex == 1) {
            [[AppManager defaultmanager].favorSongList removeObject:songTag];
            [AppManager save];
            [self.tableYeuThich reloadData];
            songTag = @"";
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
